package apy.codecase.greendao;

import android.content.Context;
import android.util.Log;
import org.greenrobot.greendao.query.QueryBuilder;
import java.util.List;

import apy.codecase.greendao.gen.PswDaoModelDao;
import apy.codecase.model.PswDaoModel;

/**
 * Created by Administrator on 2017/10/21.
 */

public class PswDaoUtils {
    private static final String TAG = PswDaoUtils.class.getSimpleName();
    private DaoManager mManager;

    public PswDaoUtils(Context context){
        mManager = DaoManager.getInstance();
        mManager.init(context);
    }

    /**
     * 完成UserDaoModel记录的插入，如果表未创建，先创建UserDaoModel表
     * @param UserDaoModel
     * @return
     */
    public boolean insertUserDaoModel(PswDaoModel UserDaoModel){
        boolean flag = false;
        flag = mManager.getDaoSession().getPswDaoModelDao().insert(UserDaoModel) == -1 ? false : true;
        Log.i(TAG, "insert PswDaoModel :" + flag + "-->" + UserDaoModel.toString());
        return flag;
    }

    /**
     * 插入多条数据，在子线程操作
     * @param UserDaoModelList
     * @return
     */
    public boolean insertMultUserDaoModel(final List<PswDaoModel> UserDaoModelList) {
        boolean flag = false;
        try {
            mManager.getDaoSession().runInTx(new Runnable() {
                @Override
                public void run() {
                    for (PswDaoModel UserDaoModel : UserDaoModelList) {
                        mManager.getDaoSession().insertOrReplace(UserDaoModel);
                    }
                }
            });
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 修改一条数据
     * @param UserDaoModel
     * @return
     */
    public boolean updateUserDaoModel(PswDaoModel UserDaoModel){
        boolean flag = false;
        try {
            mManager.getDaoSession().update(UserDaoModel);
            flag = true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 删除单条记录
     * @param UserDaoModel
     * @return
     */
    public boolean deleteUserDaoModel(PswDaoModel UserDaoModel){
        boolean flag = false;
        try {
            //按照id删除
            mManager.getDaoSession().delete(UserDaoModel);
            flag = true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 删除所有记录
     * @return
     */
    public boolean deleteAll(){
        boolean flag = false;
        try {
            //按照id删除
            mManager.getDaoSession().deleteAll(PswDaoModel.class);
            flag = true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 查询所有记录
     * @return
     */
    public List<PswDaoModel> queryAllUserDaoModel(){
        return mManager.getDaoSession().loadAll(PswDaoModel.class);
    }

    /**
     * 根据主键id查询记录
     * @param key
     * @return
     */
    public PswDaoModel queryUserDaoModelById(long key){
        return mManager.getDaoSession().load(PswDaoModel.class, key);
    }

    /**
     * 使用native sql进行查询操作
     */
    public List<PswDaoModel> queryUserDaoModelByNativeSql(String sql, String[] conditions){
        return mManager.getDaoSession().queryRaw(PswDaoModel.class, sql, conditions);
    }

    /**
     * 使用queryBuilder进行查询
     * @return
     */
    public List<PswDaoModel> queryUserDaoModelByQueryBuilder(long id){
        QueryBuilder<PswDaoModel> queryBuilder = mManager.getDaoSession().queryBuilder(PswDaoModel.class);
        return queryBuilder.where(PswDaoModelDao.Properties._id.eq(id)).list();
    }
    public List<PswDaoModel> queryUserDaoModelByQueryBuilder(String id){
        QueryBuilder<PswDaoModel> queryBuilder = mManager.getDaoSession().queryBuilder(PswDaoModel.class);
        return queryBuilder.where(PswDaoModelDao.Properties._id.eq(id)).list();
    }

    public List<PswDaoModel> queryUserDaoModelByQueryBuilder1(String id){
        QueryBuilder<PswDaoModel> queryBuilder = mManager.getDaoSession().queryBuilder(PswDaoModel.class);
        return queryBuilder.where(PswDaoModelDao.Properties.Id.eq(id)).list();
    }

}
